package com.frame.controller;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.excel.EasyExcel;

import com.alipay.api.domain.LendingDataList;
import com.alipay.api.domain.QipanMerchantCrowd;
import com.frame.config.Result;
import com.frame.domain.Logistics;
import com.frame.service.LogisticsService;
import com.github.pagehelper.PageInfo;
import org.apache.kafka.common.internals.Topic;
import org.apache.kafka.common.network.Send;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.io.IOException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/logistics")
public class LogisticsController {

    @Autowired
    private LogisticsService logisticsService;


    @RequestMapping("/qianshou")
    public Result qianshou(Integer id)
    {
        boolean b=logisticsService.updateqianshou(id);
        return b?Result.success():Result.fail();
    }
    @RequestMapping("/getjq")
    public Result getjq(Integer id)
    {
        boolean b=logisticsService.updatejq(id);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("/updateqs")
    public Result updateqs(Integer id)
    {
        boolean b=logisticsService.updateqs(id);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("/getLogisticsInfo")
    public Result getLogisticsInfo(@RequestBody Logistics logistics, @RequestParam(defaultValue = "1") Integer pageNum, Integer pageSize) {
        PageInfo<Logistics> list = logisticsService.getLogisticsInfo(logistics, pageNum, pageSize);
        return Result.success(list);
    }

    @PostMapping("/saveUpdateLogistics")
    public Result SaveUpdateLogistics(@RequestBody Logistics logistics) {
        logisticsService.SaveUpdateLogistics(logistics);
        return Result.success();
    }

    @GetMapping("/deleteLogistics")
    public Result DeleteLogistics(Integer id) {
        logisticsService.deleteLogistics(id);
        return Result.success();
    }

    @GetMapping("/updateStatus")
    public Result updateStatus(Integer id, String status) {
        logisticsService.updateStatus(id, status);
        return Result.success();
    }

    @GetMapping("/getRawMaterialInfo")
    public Result getRawMaterialInfo() {
        List<Logistics> logistics = logisticsService.getRawMaterialInfo();
        return Result.success(logistics);
    }

    @GetMapping("exLogistics")
    public void download(HttpServletResponse response) throws IOException {
        List<Logistics> logistics = logisticsService.getList();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("物流运输", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Logistics.class).sheet("物流运输").doWrite(logistics);
    }

}